package com.googlecode.aleverr.yafotkiclient.ui.routines;

import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import com.googlecode.aleverr.yafotkiclient.api.Photo;

public class PhotoLoader extends Thread {

    public static interface OnPhotoLoadedListener {
        void onPhotoLoaded(View view, Bitmap bitmap);
    }

    private Handler handler;

    @Override
    public void run() {
        Looper.prepare();
        handler = new Handler();
        Looper.loop();
    }

    public synchronized void queuePhotoLoad(final View view, final Photo photo,
                                            final OnPhotoLoadedListener listener) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                synchronized (view) {
                    Bitmap bitmap = photo.getXSBitmap();
                    if (listener != null) {
                        listener.onPhotoLoaded(view, bitmap);
                    }
                }
            }
        });
    }
}
